﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data;
using System.Data.Objects.DataClasses;
using IRC.IRepairClouds.DataLayer.Cache;

namespace IRC.IRepairClouds.DataLayer
{
    public static partial class EntityCached
    {
        public static EUser CachedEUser(this ICloudRepairEntities dbContext, Guid userId)
        {
            return CreateCached(dbContext, i => i.Users, i => i.ID, userId);
        }

        public static EUser CachedEUser(this ICloudRepairEntities dbContext, EUser entity)
        {
            return CachedEUser(dbContext, entity.ID);
        }

        public static EUser CachedEUser(this ICloudRepairEntities dbContext, string login)
        {
            var ud = CacheManager.Users[login];
            if (ud != null)
                return CachedEUser(dbContext, ud.Id);
            return null;
        }

        public static EUser CachedEUser(this ICloudRepairEntities dbContext)
        {
            var ud = CacheManager.Users.Current;
            return CachedEUser(dbContext, ud.Id);
        }

        public static EUser CachedEntity(this EntityReference<EUser> entityReference, ICloudRepairEntities dbContext)
        {
            return CachedEUser(dbContext, entityReference.GetKey());
        }

        public static UserCached Cached(this EntityReference<EUser> entityReference)
        {
            return CacheManager.Users.All.Where(g=>g.Id==entityReference.GetKey()).FirstOrDefault();
        }
    }
}
